<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" :before-close="handleCancel" center width="800px">
    <el-form ref="editFormRef" :model="editForm" :rules="rules" size="small">
      <el-form-item label="学年名称" :label-width="labelWidth" prop="year" class="line">
        <el-input v-model="editForm.year" auto-complete="off" disabled></el-input>
      </el-form-item>
      <el-form-item label="学期" :label-width="labelWidth" prop="term" class="line">
          <el-select v-model="editForm.term"
            clearable
            placeholder="请选择课程类型"
            filterable
            style="width: 100%"
          >
            <el-option
              v-for="item in termList"
              :key="item.id"
              :label="item.term"
              :value="item.term"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="开始时间" :label-width="labelWidth" prop="startTime" class="line">
        <el-date-picker
        style="width: 100%"
        v-model="editForm.startTime"
        type="date"
        placeholder="选择日期"
        size="small"
        @change="changeTime"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" :label-width="labelWidth" prop="endTime" class="line">
        <el-date-picker
        style="width: 100%"
        v-model="editForm.endTime"
        type="date"
        placeholder="选择日期"
        size="small"
        @change="changeTime"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否当前学期" :label-width="labelWidth" prop="status" class="line">
          <el-select v-model="editForm.status"
            clearable
            placeholder="请选择"
            filterable
            style="width: 100%"
            size="small"
            >
            <el-option
              v-for="item in startList"
              :key="item.id"
              :label="item.label"
              :value="item.id"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="学期类型" :label-width="labelWidth" prop="termType" class="line">
          <el-select
            v-model="editForm.termType"
            clearable
            placeholder="请选择学期类型"
            filterable
            style="width:100%"
          >
            <el-option
              v-for="item in termTypeList"
              :key="item.id"
              :label="item.label"
              :value="item.id"
            ></el-option>
          </el-select>
      </el-form-item>
            <!-- <el-form-item label="状态" :label-width="labelWidth" prop="termStatus" class="line">
          <el-select v-model="editForm.termStatus"
            clearable
            placeholder="请选择状态"
            filterable
            style="width: 100%"
          >
            <el-option
              v-for="item in statusList"
              :key="item.id"
              :label="item.status"
              :value="item.id"
            ></el-option>
          </el-select>
      </el-form-item> -->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel" size="small">取 消</el-button>
      <el-button type="primary" @click="hanldeSave('editFormRef')" size="small">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Vue from 'vue'
import { addTermInfoList, updateTermInfoList } from '@/views/children/basic/js/termInfo'
import pick from 'lodash.pick'
import { USER_INFO } from '@/views/children/workOrder/js/constants'
import moment from 'moment'
export default {
  name: 'termInfoModal',
  data () {
    return {
      tenantNo: Vue.ls.get(USER_INFO).tenantNo,
      title: '新增学期',
      isAdd: false,
      disabled: false,
      dialogVisible: false,
      labelWidth: '120px',
      editForm: {
        year: '',
        term: '',
        status: 0,
        termType: '',
        startTime: '',
        endTime: ''
      },
      model: {},
      rules: {
        startTime: [{ required: true, message: '请选择开始时间!' }],
        endTime: [{ required: true, message: '请选择结束时间!' }],
        term: [{ required: true, message: '请选择学期' }],
        status: [{ required: true, message: '请选择是否当前学期' }],
        termType: [{ required: true, message: '请选择学期类型' }],
        termStatus: [{ required: true, message: '请选择学期状态' }]
      },
      statusList: [{ id: 0, status: '进行中' }, { id: 1, status: '已结束' }, { id: 2, status: '未开始' }],
      termList: [{ id: 1, term: '第一学期' }, { id: 2, term: '第二学期' }],
      startList: [{ id: 0, label: '是' }, { id: 1, label: '否' }],
      termTypeList: [{ id: 1, label: '秋季学期' }, { id: 2, label: '春季学期' }, { id: 3, label: '夏季学期' }, { id: 9, label: '其他学期' }]
    }
  },
  watch: {
    editForm: {
      handler (newVal, oldVal) {
        // if (this.num === 0) {
        if (this.editForm.status === 0) {
          this.editForm.status = 0
        } else {
          this.editForm.status = 1
        }
        // }
        // // console.log(newVal)
        // this.num++
      }
    }
  },
  methods: {
    add () {
      this.edit({})
    },
    edit (record) {
      this.dialogVisible = true
      this.model = Object.assign({}, record)
      console.log(record)
      this.$nextTick(() => {
        if (record.id || record.id === 0) {
          this.title = '修改学期'
          if (!this.model.termStatus) {
            this.model.termStatus = this.model.status
          }
          this.editForm = pick(this.model, 'year', 'term', 'startTime', 'endTime', 'status', 'termType', 'termStatus')
        } else {
          this.title = '新增学期'
          // this.editForm.insideNotice = 0
          // this.editForm.mobileNotice = 0
        }
      })
    },
    // 提交表单
    hanldeSave (editFormRef) {
      this.$refs[editFormRef].validate(async valid => {
        if (valid) {
          let res, msg, sts
          if (this.editForm.status === 0) {
            sts = 1
          } else {
            sts = 0
          }
          const values = {
            term: this.editForm.term,
            startTime: moment(this.editForm.startTime).format('YYYY-MM-DD').toString(),
            endTime: moment(this.editForm.endTime).format('YYYY-MM-DD').toString(),
            termType: parseInt(this.editForm.termType),
            status: sts
            // termStatus: this.editForm.termStatus
          }
          console.log(values)
          if (!this.model.id) {
            console.log(values)
            res = await addTermInfoList(Object.assign({}, values))
            msg = '保存成功！'
          } else {
            console.log(Object.assign({}, values, { id: this.model.id }))
            res = await updateTermInfoList(Object.assign({}, values, { id: this.model.id }))
            msg = '修改成功！'
          }
          if (res.code === 0) {
            this.$message.success(msg)
          } else {
            this.$message.warning(res.msg)
          }
          this.$emit('ok')
          this.handleCancel()
        }
      })
    },
    statusReturn () {
      if (this.editForm.status === 0) {
        return 1
      } else if (this.editForm.status === 1) {
        return 0
      } else if (this.editForm.status === 2) {
        return 0
      } else {
        return 0
      }
    },
    handleCancel () {
      this.dialogVisible = false
      this.model = {}
      this.$refs.editFormRef.resetFields()
    },
    validateTypeName (rule, value, callback) {
      if (value) {
        if (value.toString().trim() === '') {
          callback(new Error('请输入类型名称'))
        } else if (value.length > 30) {
          callback(new Error('类型名称不能超过30个字符'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    },
    validateTypeRemark (rule, value, callback) {
      if (value) {
        if (value.toString().trim() === '') {
          callback(new Error('请输入类型备注'))
        } else if (value.length > 80) {
          callback(new Error('类型备注不能超过80个字符'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    },
    changeTime () {
      let start = moment(this.editForm.startTime).format('YYYY').toString()
      let end = moment(this.editForm.endTime).format('YYYY').toString()
      this.editForm.year = start + '~' + end + '学年'
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-footer {
  text-align: right;
}

/deep/ .el-rate__item {
  padding-top: 5px;
  .el-rate__icon {
    font-size: 22px;
  }
}
/deep/ .img-over {
  margin-right: 5px;
  width: 60px;
  height: 45px;
  cursor: pointer;
}
/deep/ .viewer-container {
  z-index: 3000 !important;
}
.line{
  display: inline-block;
  width: 350px;
}
</style>
